/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.io.File;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.TimingScore;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.boxin.ims.modules.wechat.dao.TimingScoreDao;

/**
 * 定点扫描获得积分Service
 * @author Jakemanse
 * @version 2014-05-05
 */
@Component
@Transactional(readOnly = true)
public class TimingScoreService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(TimingScoreService.class);
	
	@Autowired
	private TimingScoreDao timingScoreDao;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	public TimingScore get(Long id) {
		return timingScoreDao.findOne(id);
	}
	
	public Page<TimingScore> find(Page<TimingScore> page, TimingScore timingScore) {
		DetachedCriteria dc = timingScoreDao.createDetachedCriteria();
		if (timingScore.getUser()!=null && timingScore.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", timingScore.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(timingScore.getName())){
			dc.add(Restrictions.like("name", "%"+timingScore.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return timingScoreDao.find(page, dc);
	}
	
	
	
	@Transactional(readOnly = false)
	public void save(TimingScore timingScore) {
		timingScore.setContent(StringEscapeUtils.escapeHtml4(timingScore.getContent()));
		timingScoreDao.save(timingScore);
		
	}
	
	
	
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		timingScoreDao.deleteById(id);
	}
	
}
